package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.BarrioDAORemote;
import proyecto.EntityBeans.Barrio;


@Stateless
@Remote(BarrioBLRemote.class)
public class BarrioBL implements BarrioBLRemote {

	BarrioDAORemote barrioDAORemote;
	
	public int save(Barrio barrio) {	
		
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return barrioDAORemote.save(barrio); //alta de barrio		
		
	}
		
	public Barrio findById(int id) {
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return barrioDAORemote.findById(id);
		
	}
	
		
	public void merge(Barrio barrio) {
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
			barrioDAORemote.merge(barrio);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
	public List findByName(String name)
	{	
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return barrioDAORemote.findByName(name);
	}
	
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return barrioDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return barrioDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idBarrio){	
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		barrioDAORemote.remove(idBarrio);
	}
	
	public void refresh(Barrio barrio){
		try
		{
			Context context = new InitialContext();
			barrioDAORemote = (BarrioDAORemote) context.lookup("BarrioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		barrioDAORemote.refresh(barrio);
	}
}
